package com.cuitrip.business.login.proxy;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import com.cuitrip.service.R;
import com.lab.logtrack.AppLog;
import com.lab.utils.LogHelper;
import com.lab.utils.MessageUtils;
import com.lab.utils.SmsSdkHelper;
import com.lab.utils.ToastUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import cn.smssdk.EventHandler;
import cn.smssdk.SMSSDK;

/**
 * 登录相关逻辑
 * Created by ak on 15/9/23.
 */
public class VcodeProxy{

    private static VcodeProxy sInstance;
    private static final int UPDATE_TIME = 1;
    private static final int DURATION = 30;

    // 默认使用中国区号
    private static final String DEFAULT_COUNTRY_ID = "42";

    public static VcodeProxy getInstance() {
        if (null == sInstance)
            sInstance = new VcodeProxy();
        return sInstance;
    }

    public @Nullable String getCountryCodeByCountry(String country){
        String[] countryArray = SMSSDK.getCountry(country);
        if (country != null) {
            String currentCode = countryArray[1];
        }
        return null;
    }

    public String getCurrentCountryCode(Context context){
        String[] country = getCurrentCountry(context);
        if (country != null) {
            String currentCode = country[1];
            return currentCode;
        }
        return null;
    }

    private String[] getCurrentCountry(Context context) {
        String mcc = getMCC(context);
        String[] country = null;
        if (!TextUtils.isEmpty(mcc)) {
            country = SMSSDK.getCountryByMCC(mcc);
        }

        if (country == null) {
            LogHelper.w("SMSSDK", "no country found by MCC: " + mcc);
            country = SMSSDK.getCountry(DEFAULT_COUNTRY_ID);
        }
        return country;
    }

    private String getMCC(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        // 返回SIM卡运营商所在国家的MCC+MNC. 5位或6位. 如果没有SIM卡返回空
        String temp = tm.getNetworkOperator();
        if (!TextUtils.isEmpty(temp)) {
            return temp;
        }

        // 返回当前手机注册的网络运营商所在国家的MCC+MNC. 如果没注册到网络就为空.
        temp = tm.getSimOperator();
        if (!TextUtils.isEmpty(temp)) {
            return temp;
        }


        return "";
    }


}
